/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.tmodel.EvaluateBatch;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiFindOne;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(EvaluateBatch.class)
@Component
public class EvaluateBatchRepository extends BaseRepository<EvaluateBatch> {

	@Autowired
	NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(EvaluateBatch.class);
	}

	@Override
	@JsonApiSave
	public EvaluateBatch save(EvaluateBatch entity) {
		if (entity.getId() == null) {
			entity = nursingService.createEvaluateBatchOnSource(entity);
			return entity;
		} else {
			if (entity.getDelStatus() == 1) {
				return nursingService.deleteEvaluateBatch(entity);
				//return super.save(entity);
			} else {
				// 仅限于最终确定提交 进行计算每个result的得分 计算每个模版是否答完
				// （答完计算得分各个result等级以及batch等级
				// 未打完只要得分 等级 设置未完成）
				entity = nursingService.createEvaluateBatchLevelData(entity,true);
				return entity;
			}

		}

	}

	@Override
	@JsonApiFindAll
	public Iterable<EvaluateBatch> findAll(final QueryParams requestParams) {
		Iterable<EvaluateBatch> dataList = super.findAll(requestParams);
//		for (EvaluateBatch batch : dataList) {
//			nursingService.getEvaluateBatchData(batch);
//		}
		dataList=nursingService.getEvaluateBatchDataAll(dataList); 
		return dataList;
	}

	@Override
	@JsonApiFindOne
	public EvaluateBatch findOne(final Long id, final QueryParams requestParams) {
		// 初始化batch 各个list数值
		// 查询计算每个result的得分 计算每个模版是否答完 （答完计算得分各个result等级以及batch等级
		// 未打完只要得分 等级 设置未完成）
		EvaluateBatch batch = (EvaluateBatch) dslJpaService.findById(id);
		//batch = nursingService.getEvaluateBatchData(batch);
		return batch;
	}
}
